function Zoom() {
	this.topX = 0;
	this.topY = 0;
	
	this.bottomX = 0;
	this.bottomY = 0;
	
	this.active = false;
	
	this.getTopX = function() {
		if (this.bottomX<this.topX)
			return this.bottomX;
		else 
			return this.topX;
	}
	
	this.getTopY = function() {
		if (this.bottomY < this.topY)
			return this.bottomY;
		else 
			return this.topY;
	}
	
	this.getBottomX = function() {
		if (this.bottomX<this.topX)
			return this.topX;
		else 
			return this.bottomX;
	}
	
	this.getBottomY = function() {
		if (this.bottomY<this.topY)
			return this.topY;
		else 
			return this.bottomY;
	}
	
	this.getCurrentHeight = function() {
		if((this.bottomY - this.topY)<0)
			return ((this.bottomY - this.topY))*(-1);
		else 
			return this.bottomY - this.topY;
	}
	
	this.getCurrentWidth = function() {
		if ((this.bottomX - this.topX)<0)
			return (this.bottomX - this.topX)*(-1);
		else
			return this.bottomX - this.topX;
	}
	
	this.setTopX = function(value) {
		this.topX = value;
	}
	
	this.setTopY = function(value) {
		this.topY = value;
	}
	
	this.setTopXY = function(valueX, valueY) {
		this.topX = valueX;
		this.topY = valueY;
	}
	
	this.setCurrentX = function(value) {
		this.bottomX = value;
	}
	
	this.setCurrentY = function(value) {
		this.bottomY = value;
	}
	
	this.setCurrentXY = function(valueX, valueY) {
		this.bottomX = valueX;
		this.bottomY = valueY;
	}
	
	this.start = function() {
		this.active = true;
	}
	
	this.stop = function() {
		this.active = false;
	}
	
	this.isActive = function () {
		return this.active;
	}
}

/**
 * Draws grid and curve
 * @param context Canvas context used for drawing.
 * @param valuse Values to be used for drawing the curve
 * @param offset Specifies X axis offset. Only values greater then, or equal to 0 are allowed.
 **/
function drawGridAndCurve(element, context, values, offset) {
	drawGrid(element, context, 5, 5, "#eeeeee", offset % 5);
	context.stroke();
	drawGrid(element, context, 50, 50, "#000000", offset % 50);
	context.stroke();
	drawLine(element, context, values, offset);
	context.stroke();
}

//Clears the canvas
//	@param Element the element to be cleared
function clear(element)  {
	element.width = element.width;
}
	


//Draw line specified in the measurements array.
//	@param measurementValues Values to be drawn. Values are stored into property measurements
//		for later usage. If measurementValues is null then use the measurementValues
//		stored in measurement property.
function drawLine(canvasElement, ctx, measurementValues, offset) {
	
	//if no values are sent, reuse the old ones.
	if (measurementValues != null)
		drawLine.storedMeasurements = measurementValues;
	
	ctx.beginPath();
	ctx.strokeStyle = "#ff3333";
	var measurements;
	var startTime = new Date();
	var currentTime;
	drawLine.storedMeasurements.length > (offset + canvasElement.width) ? measurements = drawLine.storedMeasurements.slice(offset, offset + canvasElement.width) : measurements = drawLine.storedMeasurements.slice(offset);
	startTime.setTime(measurements[0].x);	
	
	for (var m in measurements) {
		currentTime = new Date();
		currentTime.setTime(measurements[m].x);

		ctx.lineTo((currentTime.getTime() - startTime.getTime())/10 + 0.5, 480 - measurements[m].y + 0.5);
		ctx.moveTo((currentTime.getTime() - startTime.getTime())/10 + 0.5, 480 - measurements[m].y + 0.5);			
		
		//write the appropriate time stamp.
		ctx.font = "bold 10px arial";
		if ((currentTime.getTime() - startTime.getTime()) % 1000 == 0)
			ctx.fillText(currentTime.getMinutes() + ":" + currentTime.getSeconds(), (currentTime.getTime() - startTime.getTime())/10 - 7, canvasElement.height - 20);
	}
	ctx.closePath();
}

//Draw grid with specific offset and X and Y axis cusomizations
//	@param canvasElement HTML Canvas element
//	@param ctx Canvas context to be used
//	@param xAxis number of pixels between two X axis lines
//	@param yAxis number of pixels between two Y axis lines
//	@param lineColor color to be used for drawing lines
//	@param offset number of pixels to offset (virtual) x=0 line
function drawGrid(canvasElement, ctx, xAxis, yAxis, lineColor, offset) {
	ctx.strokeStyle = lineColor;
	ctx.beginPath();
	for (x=0.5; x<canvasElement.width + offset; x += xAxis) {
		ctx.moveTo(x - offset, 0);
		ctx.lineTo(x - offset, canvasElement.height);
	}
	 
	for (y=canvasElement.height - 0.5 ; y>0; y -=yAxis) {
		ctx.moveTo(0, y);
		ctx.lineTo(canvasElement.width, y);
	}
	ctx.closePath();
}